﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace P2P_Backup.archive
{
    public class ArchivedFile : File
    {
        public ArchivedFile(RemotePeer peer, string fileId, string fileSize)
        {
            Guid.TryParse(fileId, out this.Id);
            long.TryParse(fileSize, out this.Size);
            this.BackupPeer = peer;
            this.Path = String.Format(@"{0}\{1}", peer.Hostname, fileId);
            this.Path = System.IO.Path.Combine(P2PBackup.Instance.ArchivePath, this.Path);
        }

        public override Stream GetWritableStream()
        {            
            Console.WriteLine("Saving {0} to Archive", this.Name());
            Directory.CreateDirectory(System.IO.Path.GetDirectoryName(this.Path));

            FileStream fs = new FileStream(this.Path, FileMode.Create); //tools.io.FileTools.WaitAndOpenFile(this.Path,FileAccess.Write);
            return fs;
        }

        public override Stream GetReadableStream()
        {
            FileStream fs = new FileStream(this.Path, FileMode.Open);
            return fs;
        }
    }
}
